package po

import (
	"encoding/json"
	"time"
)

type Instance struct {
	Id       int64  `json:"id"`
	GUID     string `xorm:"guid" json:"guid"`
	ConfigID int64  `xorm:"config_id" json:"config_id"`
	Name     string `xorm:"name" json:"name"`
	Describe string `xorm:"describe" json:"describe"`
	Kind     string `xorm:"kind" json:"kind"`
	Property string `xorm:"property" json:"property"`
	Version  int64  `xorm:"version" json:"version"`
	Platform string `xorm:"platform" json:"platform"`
	Origin   string `xorm:"origin" json:"origin"`

	//DetailRepoKey    string          `xorm:"detail_repo_key"`
	DetailRepoValue ConfigDetail `xorm:"detail" json:"detail"`
	//RunTimeRepoKey   string          `xorm:"runtime_repo_key"`
	RunTimeRepoValue InstanceRuntime `xorm:"runtime" json:"runtime"`
	Created          time.Time       `xorm:"created" json:"created"`
	Updated          time.Time       `xorm:"updated" json:"updated"`
	Deleted          time.Time       `xorm:"deleted" json:"deleted"`
}

type InstanceRuntime struct {
	ClusterName      string            `json:"cluster_name" yaml:"cluster_name"`             // 集群名称
	ClusterAgentGuid string            `json:"cluster_agent_guid" yaml:"cluster_agent_guid"` // 集群代理guid
	AppState         int               `json:"app_state" yaml:"app_state"`
	PodsCount        int64             `json:"pods_count" yaml:"pods_count"` // pod数量
	PodsState        map[string]int    `json:"pods_state" yaml:"pods_state"` // pod状态
	PodsService      map[string]string `json:"pods_service" yaml:"pods_service"`
	PodsOnNodes      map[string]string `json:"pods_on_nodes" yaml:"pods_on_nodes"` // pod运行在集群那个node上

	ContainerCount  int64             `json:"container_count" yaml:"container_count"`     // 容器数量
	ContainerInPods map[string]string `json:"container_in_pods" yaml:"container_in_pods"` // 容器在运行在哪个pod里面
}

func (r *InstanceRuntime) FromDB(data []byte) error {
	return json.Unmarshal(data, r)
}

func (r *InstanceRuntime) ToDB() ([]byte, error) {
	return json.Marshal(r)
}

type InstanceToUser struct {
	Id       int64
	ConfigId int64  `xorm:"instance_id"`
	UserId   string `xorm:"user_id"`
	GroupId  int64  `xorm:"group_id"`
}

func (r InstanceToUser) TableName() string {
	return "srvp_instance_to_user"
}
